package leetcode_301_400;

public class LeeCode_376 {
    public static void main(String[] args) {

    }
    private static int wiggleMaxLength2(int[] nums) {
        int n = nums.length;
        int[] up = new int[n];
        int[] down = new int[n];
        up[0] = 1;
        down[0] = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] == nums[i - 1]){
                up[i] = up[i - 1];
                down[i] = down[i - 1];
            }else if (nums[i] > nums[i - 1]){
                up[i] = Math.max(up[i - 1], down[i - 1] + 1);
                down[i] = down[i - 1];
            }else {
                down[i] = Math.max(up[i - 1] + 1, down[i - 1]);
                up[i] = up[i - 1];
            }
        }
        return Math.max(up[n - 1], down[n - 1]);
    }
    private static int wiggleMaxLength(int[] nums) {
        int n = nums.length;
        int[] maxes = new int[n];
        int[] state = new int[n];
        maxes[0] = 1;
        int ans = 1;
        for (int i = 1; i < n; i++) {
            int sta = 0;
            int max = 0;
            for (int j = 0; j < i; j++) {
                if (nums[i] == nums[j]){
                    continue;
                }
                if (state[j] == 0){
                    if (maxes[j] + 1 > max){
                        max = maxes[j] + 1;
                        sta = nums[j] > nums[i] ? -1 : 1;
                    }
                }else {
                    if (nums[i] > nums[j] && state[j] == -1 && maxes[j]  + 1 > max){
                        max = maxes[j] + 1;
                        sta = 1;
                    } else if (nums[i] < nums[j] && state[j] == 1 && maxes[j] + 1 > max) {
                        max = maxes[j] + 1;
                        sta = -1;
                    }
                }
            }
            maxes[i] = max;
            state[i] = sta;
            ans = Math.max(ans, max);
        }
        return ans;
    }
}
